package android.coocoo.views;

import java.util.ArrayList;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.coocoo.common.Const;
import android.coocoo.common.XmlParser;
import android.coocoo.models.CategoryInfo;
import android.coocoo.models.TaskInfo;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class DisplayTaskInfoActivity extends Activity{
	
	Resources res;
	TaskInfo task;
	ArrayList<CategoryInfo> categoriesList;
	ImageView btSaveTask;
	EditText edTitle;
	Spinner spCategory;
	Spinner spPriority;
	EditText etDescription;
	CheckBox cbAlarm;
	CheckBox cbVibration;
	TextView btStartTime;
	TextView btDate;
	
	int starttime_hour;
	int starttime_minute;
	int starttime_day;
	int starttime_month;
	int starttime_year;
	
	static final int STARTTIME_DIALOG = 0;
    static final int DATE_DIALOG = 1;
    
    static final int UPDATE_DISPLAY_STARTTIME = 10;
    static final int UPDATE_DISPLAY_STARTDATE = 11;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display_task_info);
//		String _test = i.getExtras().getString("test_data");
//		Log.e("test_data", _test);
//		task = i.getExtras().getParcelable(res.getString(R.string.TASK));
		task = (TaskInfo)getIntent().getParcelableExtra("task");
//		task = new TaskInfo();
//		categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
		categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
//		categoriesList = null;
		Log.e("pass_flag", "add new task");
		
		edTitle = (EditText)findViewById(R.id.etTitle);
		edTitle.setText(task.getTitle());
		
		spCategory = (Spinner)findViewById(R.id.spCategory);
		String[] arrSpCategory = new String[categoriesList.size()];
		int i;
		for (i=0; i<categoriesList.size(); i++)
			arrSpCategory[i]=categoriesList.get(i).getTitle();
		ArrayAdapter<CharSequence> adSpCategory = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, arrSpCategory);
		adSpCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spCategory.setAdapter(adSpCategory);
		int position=categoriesList.get(0).getId();
		for (i=0; i<categoriesList.size(); i++)
			if (categoriesList.get(i).getId()==task.getCategoryId())
				position=i;
		spCategory.setSelection(position);
		spCategory.setOnItemSelectedListener(new MyCategoryOnItemSelectedListener());
		
		spPriority = (Spinner)findViewById(R.id.spPriority);
		String[] arrSpPriority = new String[]{"Normal", "Medium", "High"};
		ArrayAdapter<CharSequence> adSpPriority = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, arrSpPriority);
		adSpPriority.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spPriority.setAdapter(adSpPriority);
		spPriority.setSelection(task.getPriority());
		spPriority.setOnItemSelectedListener(new MyPriorityOnItemSelectedListener());
		
		etDescription = (EditText)findViewById(R.id.etDescription);
		etDescription.setText(task.getDescription());
		
		cbAlarm = (CheckBox)findViewById(R.id.cbAlarm);
		cbAlarm.setChecked(task.isHasAlarm());
		cbVibration = (CheckBox)findViewById(R.id.cbVibration);
		
		starttime_day = task.getStartTime().getDate();
		starttime_month = task.getStartTime().getMonth();
		starttime_year = task.getStartTime().getYear();
		starttime_hour = task.getStartTime().getHours();
		starttime_minute = task.getStartTime().getMinutes();
		
		btDate = (TextView)findViewById(R.id.btDate);
		btDate.setText(task.getStartTime().getDate()
						+ " / "
						+ (task.getStartTime().getMonth()+1)
						+ " / "
						+ task.getStartTime().getYear());
		btDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG);
			}
		});
		btStartTime = (TextView)findViewById(R.id.btStartTime);
		btStartTime.setText(task.getStartTime().getHours()
							+ " : "
							+ task.getStartTime().getMinutes());
		btStartTime.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(STARTTIME_DIALOG);
			}
		});
		cbAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
				if (isChecked){
					btDate.setEnabled(true);
					btStartTime.setEnabled(true);
					cbVibration.setEnabled(true);
				}else{
					btDate.setEnabled(false);
					btStartTime.setEnabled(false);
					cbVibration.setEnabled(false);
				}
			}
		});
		
		if (task.isHasAlarm()){
			cbVibration.setChecked(task.isVibration());
		}else{
			cbVibration.setEnabled(false);
			btDate.setEnabled(false);
			btStartTime.setEnabled(false);
		}
		
		
		btSaveTask = (ImageView)findViewById(R.id.btSaveTask);
		btSaveTask.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				setResult(Const.Result.SUCCESS, intent);
				
				task.setTitle(edTitle.getText().toString());
				task.setDescription(etDescription.getText().toString());
				task.setHasAlarm(cbAlarm.isChecked());
				task.setVibration(cbVibration.isChecked());
				
				intent.putExtra("returnTask", task);
				finish();
			}
		});
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	public class MyPriorityOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
//	      Toast.makeText(parent.getContext(), "The Priority is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
	      task.setPriority(pos);
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    	task.setPriority(Const.PRIORITY_NORMAL);
	    }
	}
	
	public class MyCategoryOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
	      Toast.makeText(parent.getContext(), "The Category is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
	      task.setCategoryId(categoriesList.get(pos).getId());
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    	task.setCategoryId(categoriesList.get(0).getId());
	    }
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    	case STARTTIME_DIALOG:
	    		return new TimePickerDialog(this, mTimeSetListener, task.getStartTime().getHours(), task.getStartTime().getMinutes(), true);
	    	case DATE_DIALOG:
//	    		return new DatePickerDialog(this, mDateSetListener, task.getStartTime().getYear(), task.getStartTime().getMonth(), task.getStartTime().getDay());
	    		Log.e("data", task.getStartTime().getYear() + "/" + task.getStartTime().getMonth() + "/" + task.getStartTime().getDate());
	    		return new DatePickerDialog(this, mDateSetListener, task.getStartTime().getYear(), task.getStartTime().getMonth(), task.getStartTime().getDate());
//	    		return new DatePickerDialog(this, mDateSetListener, 1990, 1, 1);
	    }
	    return null;
	}
	
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
		new TimePickerDialog.OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int _hour, int _minute) {
				starttime_hour = _hour;
				starttime_minute = _minute;
				updateDisplay(UPDATE_DISPLAY_STARTTIME);
			}
		};
     // the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener =
		new DatePickerDialog.OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				starttime_year = year;
				starttime_month = monthOfYear;
				starttime_day = dayOfMonth;
				updateDisplay(UPDATE_DISPLAY_STARTDATE);
			}
		};
	
	private void updateDisplay(int mode){
		switch (mode) {
			case UPDATE_DISPLAY_STARTTIME:
				task.getStartTime().setHours(starttime_hour);
				task.getStartTime().setMinutes(starttime_minute);
				btStartTime.setText(task.getStartTime().getHours()
									+ " : "
									+ task.getStartTime().getMinutes());
				break;
			case UPDATE_DISPLAY_STARTDATE:
				task.getStartTime().setYear(starttime_year);
				task.getStartTime().setMonth(starttime_month);
				task.getStartTime().setDate(starttime_day);
				btDate.setText(task.getStartTime().getDate()
								+ " / "
								+ (task.getStartTime().getMonth()+1)
								+ " / "
								+ task.getStartTime().getYear());
				break;
			default:
				break;
		}
	}
}
